package com.ruix.app.mapper;

import com.ruix.app.model.Classes;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by hadexs on 2017/4/14.
 */

public class ClassesMapper implements ApiMapper<ArrayList<Classes>>{

    @Override
    public ArrayList<Classes> transform(JSONObject response) {
        ArrayList list = new ArrayList();
        if (response != null) {
            JSONObject data = response.optJSONObject("data");
            if (data != null) {
                JSONArray organizaNarray = data.optJSONArray("organization_list");//组织圈列表
                if (organizaNarray != null && organizaNarray.length() > 0) {
                    parseOrgnaz(list, organizaNarray);
                }
                JSONArray josNarray = data.optJSONArray("list");
                if (josNarray != null && josNarray.length() > 0) {
                    parseClasses(list, josNarray);
                }

                //排序
                Collections.sort(list,new Comparator<Classes>() {

                        @Override
                        public int compare(Classes o1, Classes o2) {
                           long onew =   o1.getLastUpdate();
                            long two =  o2.getLastUpdate();
                            if (o1.getLastUpdate() > o2.getLastUpdate()){
                                return -1;
                            } else if (onew == two){
                                return  0;
                            } else {
                                return 1;
                            }
                        }
                    });

                //list


//                Classes classesRecommend = new Classes();
//                classesRecommend.setID("recommend");
//                classesRecommend.setName("圈子推荐");
//                classesRecommend.setIcon("");
//                classesRecommend.setType("");
//                classesRecommend.setLastPost("小助手：这周新增了圈子，快来看看吧");
//                classesRecommend.setLastUpdate(0);
//                classesRecommend.setStatus("");
//                list.add(classesRecommend);
//                Classes sugest = new Classes();
//                sugest.setID("sugest_feedback");
//                sugest.setName("建议与反馈");
//                sugest.setIcon("");
//                sugest.setType("");
//                sugest.setLastPost("小助手：感谢您的反馈");
//                sugest.setLastUpdate(0);
//                sugest.setStatus("");
//                list.add(sugest);

            }
        }

        return list;
    }

    private void parseOrgnaz(ArrayList list ,JSONArray organizaNarray){
        for (int i = 0; i < organizaNarray.length();i++){
            JSONObject object =   organizaNarray.optJSONObject(i);
            if (object != null) {
                Classes classes = new Classes();
                classes.setID(object.optString("id"));
                classes.setName(object.optString("name"));
                classes.setIcon(object.optString("icon"));
                classes.setClassNum(object.optString("class_num"));
                classes.setMemberNum(object.optString("member_num"));
                classes.setNews(object.optLong("news",0));
                long lastUpdateTime = object.optLong("last_update",0)*1000;
                classes.setLastUpdate(lastUpdateTime);
                classes.setOrganizClass(true);
                list.add(classes);
            }
        }
    }


    private void parseClasses(ArrayList list ,JSONArray josNarray){
        for (int i = 0; i < josNarray.length();i++){
            JSONObject object =   josNarray.optJSONObject(i);
            if (object != null){
                Classes classes = new Classes();
                classes.setID(object.optString("id"));
                classes.setName(object.optString("name"));
                classes.setIcon(object.optString("icon"));
                classes.setType(object.optString("type"));
                classes.setLastPost(object.optString("last_post"));
                long lastUpdateTime = object.optLong("last_update",0)*1000;
                classes.setLastUpdate(lastUpdateTime);
                classes.setStatus(object.optString("status"));
                classes.setHaseNewMessage(object.optInt("new") == 1);
                list.add(classes);
            }
        }
    }

}
